<?php
namespace User\Controller;
use Think\Page;
use User\Model\OptionModel;

class ToolController extends UserController
{
    public function __construct()
    {
        parent::__construct();
        if (!headers_sent() && // 如果页面头部信息还没有输出
            extension_loaded("zlib") && // 而且php已经加载了zlib扩展
            strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")
        ) //而且浏览器接受GZIP
        {
            ini_set('zlib.output_compression', 'On');
            ini_set('zlib.output_compression_level', '4');
        }
    }

    public function option()
    {
        $page = I("get.page", 0, "");
        $first_row = $page * 1;
        $cond['status'] = OptionModel::STATUS_ACTIVE;
        $view_arr = M("option_view")->where($cond)->limit($first_row, 10)->order("view_id desc")->select();
        $this->assign("view_arr", $view_arr);
        $this->display();
    }

    public function option_del()
    {
        $cond['view_id'] = I("get.view_id", 0, "");
        M("option_view")->where($cond)->save(array("status" => OptionModel::STATUS_DELETE));
        $this->ajaxReturn("success");
    }

    public function option_dept()
    {
        $cond['status'] = OptionModel::STATUS_ACTIVE;
        $dept_arr = M("option_dept")->where($cond)->order("dept_id desc")->select();
        $this->assign("dept_arr", $dept_arr);
        $this->display();
    }

    public function option_dept_edit()
    {
        $dept_id = I("get.dept_id", "", "");
        $data['name'] = I("get.name", "", "");
        if (empty($dept_id)) {
            M("option_dept")->add($data);
        } else {
            $cond['dept_id'] = $dept_id;
            M("option_dept")->where($cond)->save($data);
        }
        $this->ajaxReturn("success");
    }

    public function option_dept_del()
    {
        $dept_id = I("get.dept_id", "", "");
        if (!empty($dept_id)) {
            $data['status'] = OptionModel::STATUS_DELETE;
            $cond['dept_id'] = $dept_id;
            M("option_dept")->where($cond)->save($data);
        }
        $this->ajaxReturn("success");
    }

    public function option_person()
    {
        $cond['status'] = OptionModel::STATUS_ACTIVE;
        $name = I("get.name", "", "");
        if (!empty($name)) {
            $cond['name'] = array("LIKE", "%" . $name . "%");
        }
        $telephone = I("get.telephone", "", "");
        if (!empty($telephone)) {
            $cond['telephone'] = array("LIKE", "%" . $telephone . "%");
        }

        $count = M("option_person")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $person_arr = M("option_person")->where($cond)->limit($page->firstRow, 10)->order("person_id desc")->select();
        $this->assign("person_arr", $person_arr);

        //---获取部门信息------
        $cond_dept['status'] = OptionModel::STATUS_ACTIVE;
        $dept_arr = M("option_dept")->where($cond_dept)->getField("dept_id, name");
        $this->assign("dept_arr", $dept_arr);
        $this->display();
    }

    public function option_person_edit()
    {
        $person_id = I("get.person_id", "", "");
        $data['name'] = I("get.name", "", "");
        $data['telephone'] = I("get.telephone", "", "");
        $data['dept_id'] = I("get.dept_id", 0, "");
        $data['level'] = I("get.level", 0, "");
        if (empty($person_id)) {
            M("option_person")->add($data);
        } else {
            $cond['person_id'] = $person_id;
            M("option_person")->where($cond)->save($data);
        }
        $this->ajaxReturn("success");
    }

    public function option_person_del()
    {
        $person_id = I("get.person_id", "", "");
        if (!empty($person_id)) {
            $data['status'] = OptionModel::STATUS_DELETE;
            $cond['person_id'] = $person_id;
            M("option_person")->where($cond)->save($data);
        }
        $this->ajaxReturn("success");
    }

    //----淘志愿报考指南开始----
    public function school()
    {
        $cond['status'] = array("NEQ", 9);

        $count = M("tzy_bkzn_school")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("tzy_bkzn_school")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('code asc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    public function schoolUpdate()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $schId = I("get.schId", '', '');
            if (empty($schId)) {
                $type = 'add';
            } else {
                $school = M("tzy_bkzn_school")->where("s_id=$schId")->find();
                $this->assign("school", $school);
                $type = 'update';
            }
            $this->assign('type', $type);
            return $this->display('schoolUpdate');
        }
        if (empty($params['name'])) {
            $this->ajaxReturn(['code' => -1, 'msg' => '请填写学校名称']);
        }

        $data['name'] = $params['name'];
        $data['code'] = intval($params['code']);
        $data['reg_date_s'] = $params['reg_date_s'];
        $data['test_date_s'] = $params['test_date_s'];
        $data['award'] = $params['award'];
        if (empty($params['schId'])) {
            M("tzy_bkzn_school")->add($data);
        } else {
            $cond_user['s_id'] = $params['schId'];
            M("tzy_bkzn_school")->where($cond_user)->save($data);
        }

        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }


    public function schoolDelete()
    {
        $schId = I("get.schId", "", "");
        $data['status'] = 9;
        if (!empty($schId)) {
            M("tzy_bkzn_school")->where(['s_id' => $schId])->save($data);
            $cond['sch_id'] = $schId;
            M("tzy_bkzn_spec")->where($cond)->save($data);
            $spec_list = M("tzy_bkzn_spec")->where($cond)->select();
            foreach ($spec_list as $spec) {
                $cond_spec['spec_id'] = $spec['spec_id'];
                M("tzy_bkzn_cond")->where($cond_spec)->save($data);
            }
        }
        return $this->my_success("成功");
    }


    public function spec()
    {
        $cond['status'] = array("NEQ", 9);
        $cond['sch_id'] = I("get.schId", "", "");
        $count = M("tzy_bkzn_spec")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("tzy_bkzn_spec")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('spec_id desc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    public function specUpdate()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $specId = I("get.specId", '', '');
            if (empty($specId)) {
                $type = 'add';
            } else {
                $spec = M("tzy_bkzn_spec")->where("spec_id=$specId")->find();
                $this->assign("spec", $spec);
                $type = 'update';
            }
            $this->assign('type', $type);
            return $this->display('specUpdate');
        }
        if (empty($params['name'])) {
            $this->ajaxReturn(['code' => -1, 'msg' => '请填写专业名称']);
        }
        $data['sch_id'] = $params['sch_id'];
        $data['name'] = $params['name'];
        $data['spec'] = $params['spec'];
        $data['num'] = $params['num'];
        $data['code'] = $params['code'];
        $data['bkcond'] = $params['bkcond'];
        $data['ocond'] = $params['ocond'];
        $data['minnum'] = $params['minnum'];
        $data['diffnum'] = $params['diffnum'];
        $data['remark'] = $params['remark'];
        if (empty($params['specId'])) {
            M("tzy_bkzn_spec")->add($data);
        } else {
            $cond_user['spec_id'] = $params['specId'];
            M("tzy_bkzn_spec")->where($cond_user)->save($data);
        }

        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }


    public function specDelete()
    {
        $specId = I("get.specId", "", "");
        $data['status'] = 9;
        if (!empty($specId)) {
            M("tzy_bkzn_spec")->where(['spec_id' => $specId])->save($data);
            $cond['spec_id'] = $specId;
            M("tzy_bkzn_cond")->where($cond)->save($data);
        }
        return $this->my_success("成功");
    }

    public function cond()
    {
        $cond['status'] = array("NEQ", 9);
        $cond['spec_id'] = I("get.specId", "", "");
        $count = M("tzy_bkzn_cond")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("tzy_bkzn_cond")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('cond_id asc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    public function condUpdate()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $condId = I("get.condId", '', '');
            if (empty($condId)) {
                $type = 'add';
            } else {
                $cond = M("tzy_bkzn_cond")->where("cond_id=$condId")->find();
                $this->assign("cond", $cond);
                $type = 'update';
            }
            $this->assign('type', $type);
            return $this->display('condUpdate');
        }
        $data['spec_id'] = $params['spec_id'];
        $data['cp'] = $params['cp'];
        $data['dp'] = $params['dp'];
        $data['ap'] = $params['ap'];
        $data['bp'] = $params['bp'];
        $data['abp'] = $params['abp'];
        $data['score'] = $params['score'];
        $data['szs'] = $params['szs'];
        $data['lss'] = $params['lss'];
        $data['dls'] = $params['dls'];
        $data['wls'] = $params['wls'];
        $data['hxs'] = $params['hxs'];
        $data['sws'] = $params['sws'];
        $data['jss'] = $params['jss'];
        $data['yys'] = $params['yys'];
        $data['yws'] = $params['yws'];
        $data['sxs'] = $params['sxs'];
        $data['sz_must'] = $params['sz_must'] ? 1 : 0;
        $data['ls_must'] = $params['ls_must'] ? 1 : 0;
        $data['wl_must'] = $params['wl_must'] ? 1 : 0;
        $data['dl_must'] = $params['dl_must'] ? 1 : 0;
        $data['hx_must'] = $params['hx_must'] ? 1 : 0;
        $data['sw_must'] = $params['sw_must'] ? 1 : 0;
        $data['js_must'] = $params['js_must'] ? 1 : 0;
        $data['sza'] = $params['sza'];
        $data['szb'] = $params['szb'];
        $data['szc'] = $params['szc'];
        $data['szd'] = $params['szd'];
        $data['lsa'] = $params['lsa'];
        $data['lsb'] = $params['lsb'];
        $data['lsc'] = $params['lsc'];
        $data['lsd'] = $params['lsd'];
        $data['dla'] = $params['dla'];
        $data['dlb'] = $params['dlb'];
        $data['dlc'] = $params['dlc'];
        $data['dld'] = $params['dld'];
        $data['wla'] = $params['wla'];
        $data['wlb'] = $params['wlb'];
        $data['wlc'] = $params['wlc'];
        $data['wld'] = $params['wld'];
        $data['hxa'] = $params['hxa'];
        $data['hxb'] = $params['hxb'];
        $data['hxc'] = $params['hxc'];
        $data['hxd'] = $params['hxd'];
        $data['swa'] = $params['swa'];
        $data['swb'] = $params['swb'];
        $data['swc'] = $params['swc'];
        $data['swd'] = $params['swd'];
        $data['jsa'] = $params['jsa'];
        $data['jsb'] = $params['jsb'];
        $data['jsc'] = $params['jsc'];
        $data['jsd'] = $params['jsd'];
        $data['yya'] = $params['yya'];
        $data['yyb'] = $params['yyb'];
        $data['yyc'] = $params['yyc'];
        $data['yyd'] = $params['yyd'];
        $data['ywa'] = $params['ywa'];
        $data['ywb'] = $params['ywb'];
        $data['ywc'] = $params['ywc'];
        $data['ywd'] = $params['ywd'];
        $data['sxa'] = $params['sxa'];
        $data['sxb'] = $params['sxb'];
        $data['sxc'] = $params['sxc'];
        $data['sxd'] = $params['sxd'];
        if (empty($params['condId'])) {
            M("tzy_bkzn_cond")->add($data);
        } else {
            $cond['cond_id'] = $params['condId'];
            M("tzy_bkzn_cond")->where($cond)->save($data);
        }
        S("spec_cond", NULL);
        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }


    public function condDelete()
    {
        $condId = I("get.condId", "", "");
        $data['status'] = 9;
        if (!empty($condId)) {
            $cond['cond_id'] = $condId;
            M("tzy_bkzn_cond")->where($cond)->save($data);
        }
        S("spec_cond", NULL);
        return $this->my_success("成功");
    }
    //----淘志愿报考指南结束----

    //--------满意度调查开始-------------
    public function satisfaction()
    {
        $cond['status'] = array("NEQ", 9);
        $count = M("satisfaction")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("satisfaction")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('id desc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    public function satUpdate()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $id = I("get.id", '', '');
            if (empty($id)) {
                $type = 'add';
            } else {
                $sta = M("satisfatction")->where("spec_id=$id")->find();
                $this->assign("sat", $sta);
                $type = 'update';
            }
            $this->assign('type', $type);
            return $this->display('satUpdate');
        }
        if (empty($params['name'])) {
            $this->ajaxReturn(['code' => -1, 'msg' => '请填写活动名称']);
        }
        $data['name'] = $params['name'];
        $data['start_date'] = $params['start_date'];
        $data['end_date'] = $params['end_date'];
        if (empty($params['id'])) {
            M("satisfaction")->add($data);
        } else {
            $cond['id'] = $params['id'];
            M("satisfaction")->where($cond)->save($data);
        }

        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }


    public function satDelete()
    {
        $id = I("get.id", "", "");
        $data['status'] = 9;
        if (!empty($id)) {
            M("satisfaction")->where(['id' => $id])->save($data);
        }
        return $this->my_success("成功");
    }
    //--------满意度调查结束-------------

    //--------部门管理开始---------------
    public function deptDelete()
    {
        $id = I("get.id", "", "");
        $data['status'] = 9;
        if (!empty($id)) {
            M("ins_dept")->where(['id' => $id])->save($data);
        }
        return $this->my_success("成功");
    }
    //--------部门管理结束-------------------------

    //--------部室管理开始---------------
    public function room()
    {
        $cond['status'] = array("NEQ", 9);

        $count = M("ins_room")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("ins_room")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('id desc')->select();

        $cond_emp_all['status'] = 1;
        $emp_all_list = M("ins_employee")->where($cond_emp_all)->getField("id, name");
        $this->assign("emp_all_list", $emp_all_list);

        $this->assign("list", $list);
        $this->display();
    }

    public function roomUpdate()
    {
        $params = I("get.", "", "");
        $data['name'] = $params['name'];
        $data['type'] = $params['type'];
        $data['emp_id'] = $params['emp_id'];
        $data['checker'] = $params['checker'];
        $data['deside'] = $params['deside'];
        $data['detail1'] = $params['detail1'];
        $data['detail2'] = $params['detail2'];
        $data['detail3'] = $params['detail3'];
        if (empty($params['id'])) {
            M("ins_room")->add($data);
        } else {
            $cond['id'] = $params['id'];
            M("ins_room")->where($cond)->save($data);
        }
        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }


    public function roomDelete()
    {
        $id = I("get.id", "", "");
        $data['status'] = 9;
        if (!empty($id)) {
            M("ins_room")->where(['id' => $id])->save($data);
        }
        return $this->my_success("成功");
    }

    public function exportRoom()
    {
        set_time_limit(0);
        vendor('phpexcel.PHPExcel');
        $objExcel = new \PHPExcel();
        $objActSheet = $objExcel->getActiveSheet();
        $objActSheet->setCellValue('A1', "部门名称");
        $objActSheet->setCellValue('B1', "制单");
        $objActSheet->setCellValue('C1', "审核");
        $objActSheet->setCellValue('D1', "审批");
        $line = 2;
        $cond['status'] = array("NEQ", 9);
        $emps = M("ins_employee")->where($cond)->getField("id,name");
        $rooms = M("ins_room")->where($cond)->select();
        foreach ($rooms as $key => $room) {
            $objActSheet->setCellValue('A' . $line, $room['name']);
            $objActSheet->setCellValue('B' . $line, array_key_exists($room['emp_id'], $emps) ?
                $emps[$room['emp_id']] : '');
            $objActSheet->setCellValue('C' . $line, array_key_exists($room['checker'], $emps) ?
                $emps[$room['checker']] : '');
            $objActSheet->setCellValue('D' . $line, array_key_exists($room['deside'], $emps) ?
                $emps[$room['deside']] : '');
            $line++;
        }
        $fileName = '部室详情' . date('Ymd') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
        $objWriter->save('php://output');
    }
    //--------部室管理结束-------------------------
    //--------员工管理开始---------------
    public function employee()
    {
        $cond['status'] = array("NEQ", 9);
        $name = I("get.name", "", "");
        if (!empty($name)) {
            $cond['name'] = array("like", "%" . $name . "%");
        }
        $phone = I("get.phone", "", "");
        if (!empty($phone)) {
            $cond['phone'] = $phone;
        }
        $dept_id = I("get.dept_id", "", "");
        if (!empty($dept_id)) {
            $dept_id_arr = array($dept_id);
            $this->get_dept_son($dept_id_arr, $dept_id);
            $cond['dept_id'] = array("IN", $dept_id_arr);
        }
        $count = M("ins_employee")->where($cond)->count();
        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("ins_employee")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('id desc')->select();
        foreach ($list as &$emp) {
            $cond_emp_dept['id'] = $emp['dept_id'];
            $dept = M("ins_dept")->where($cond_emp_dept)->find();
            $emp['dept'] = $dept['name'];
            if ($dept['parent_id'] != 0) {
                $this->emp_dept($emp, $dept['parent_id']);
            }
        }
        $this->assign("list", $list);

        //---模糊搜索部门---
        $cond_dept_all['status'] = 1;
        $dept_all_list = M("ins_dept")->where($cond_dept_all)->field("id, name")->select();
        $this->assign("dept_all_list", $dept_all_list);

        //----所有级部门----
        $cond_dept['status'] = array("NEQ", 9);
        $cond_dept['parent_id'] = 0;
        $dept_list = M("ins_dept")->where($cond_dept)->order("priority asc")->select();
        foreach ($dept_list as &$dept) {
            $this->dept_son($dept);
        }
        $this->assign("dept", $dept_list);
        //----模糊搜索员工---
        $cond_emp_all['stauts'] = 1;
        $emp_all_list = M("ins_employee")->where($cond_emp_all)->field("id, name")->select();
        $this->assign("emp_all_list", $emp_all_list);
        $this->display();
    }

    private function get_dept_son(&$dept_id_arr, $dept_id)
    {
        $cond['parent_id'] = $dept_id;
        $cond['status'] = 1;
        $depts = M("ins_dept")->where($cond)->field("id")->select();
        if (!empty($depts)) {
            foreach ($depts as $dept) {
                $dept_id_arr[] = $dept['id'];
                $this->get_dept_son($dept_id_arr, $dept['id']);
            }
        }
    }

    private function dept_son(&$dept)
    {
        $cond_value_dept['parent_id'] = $dept['id'];
        $cond_value_dept['status'] = array("NEQ", 9);
        //--二级部门--
        $son_list = M("ins_dept")->where($cond_value_dept)->order("priority asc")->select();
        if (!empty($son_list)) {
            $dept['son'] = $son_list;
            foreach ($dept['son'] as &$son) {
                $this->dept_son($son);
            }
        }
    }

    private function emp_dept(&$emp, $dept_id)
    {
        $cond_emp_dept['id'] = $dept_id;
        $dept = M("ins_dept")->where($cond_emp_dept)->find();
        $emp['dept'] = $dept['name'] . "-" . $emp['dept'];
        if ($dept['parent_id'] != 0) {
            $this->emp_dept($emp, $dept['parent_id']);
        }
    }

    public function employeeUpdate()
    {
        $params = I("get.", "", "");
        if (empty($params['name'])) {
            $this->ajaxReturn(['code' => -1, 'msg' => '请填写员工名称']);
        }
        $data['name'] = $params['name'];
        $data['phone'] = $params['phone'];
        $data['dept_id'] = $params['dept_id'];
        $data['position'] = $params['position'];

        if (empty($params['id'])) {
            M("ins_employee")->add($data);
        } else {
            $cond['id'] = $params['id'];
            M("ins_employee")->where($cond)->save($data);
        }

        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }


    public function employeeDelete()
    {
        $id = I("get.id", "", "");
        $data['status'] = 9;
        if (!empty($id)) {
            M("ins_employee")->where(['id' => $id])->save($data);
        }
        return $this->my_success("成功");
    }

    public function exportEmployee()
    {
        set_time_limit(0);
        vendor('phpexcel.PHPExcel');
        $objExcel = new \PHPExcel();
        $objActSheet = $objExcel->getActiveSheet();
        $objActSheet->setCellValue('A1', "部门名称");
        $objActSheet->setCellValue('B1', "员工名称");
        $objActSheet->setCellValue('C1', "联系电话");
        $objActSheet->setCellValue('D1', "职务");
        $line = 2;
        $cond['status'] = array("NEQ", 9);
        $depts = M("ins_dept")->where($cond)->getField("id,name");
        $emps = M("ins_employee")->where($cond)->select();
        foreach ($emps as $key => $emp) {
            $objActSheet->setCellValue('A' . $line, array_key_exists($emp['dept_id'], $depts) ?
                $depts[$emp['dept_id']] : '');
            $objActSheet->setCellValue('B' . $line, $emp['name']);
            $objActSheet->setCellValue('C' . $line, $emp['phone']);
            $objActSheet->setCellValue('D' . $line, $emp['position']);
            $line++;
        }
        $fileName = '员工详情' . date('Ymd') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
        $objWriter->save('php://output');
    }

    public function ajaxDeptUpdate()
    {
        $id = I("get.id", "", "");
        $data['name'] = I("get.name", "", "");
        $data['parent_id'] = I("get.parent_id", "", "");
        $data['emp_id'] = I("get.emp_id", "", "");
        if (!empty($id)) {
            $cond_save['id'] = $id;
            M("ins_dept")->where($cond_save)->save($data);
        } else {
            $cond_add['parent_id'] = $data['parent_id'];
            $cond_add['status'] = 1;
            $priority = M("ins_dept")->where($cond_add)->max("priority");
            $data['priority'] = $priority + 1;
            M("ins_dept")->add($data);
        }
        $this->my_success("成功");
    }

    public function ajaxDeptPriority()
    {
        $id = I("get.id", "", "");
        $type = I("get.type", "", "");
        $cond['parent_id'] = I("get.parent_id", "", "");
        $cond['status'] = 1;
        $priority = I("get.priority", "", "");
        if ($type == 1) {
            $cond['priority'] = array("LT", $priority);
            $small = M("ins_dept")->where($cond)->order("priority desc")->limit(1)->select();
            if (!empty($small) && ($small[0]['id'] != $id)) {
                $data_save['priority'] = $small[0]['priority'];
                $cond_save['id'] = $id;
                M("ins_dept")->where($cond_save)->save($data_save);

                $data_small['priority'] = $priority;
                $cond_small['id'] = $small[0]['id'];
                M("ins_dept")->where($cond_small)->save($data_small);
            }
        } else {
            $cond['priority'] = array("GT", $priority);
            $big = M("ins_dept")->where($cond)->order("priority asc")->limit(1)->select();
            if (!empty($big) && ($big[0]['id'] != $id)) {
                $data_save['priority'] = $big[0]['priority'];
                $cond_save['id'] = $id;
                M("ins_dept")->where($cond_save)->save($data_save);

                $data_big['priority'] = $priority;
                $cond_big['id'] = $big[0]['id'];
                M("ins_dept")->where($cond_big)->save($data_big);
            }
        }
        $this->my_success("成功");
    }
    //--------员工管理结束-------------------------
    //--------分配管理开始-------------------------
    public function award()
    {
        $cond['status'] = array("NEQ", 9);
        $room_id = I("get.room_id", "", "");
        if (!empty($room_id)) {
            $cond['room_id'] = $room_id;
        }
        $sdate = I("get.sdate", "", "");
        if (!empty($sdate)) {
            $cond['year'] = substr($sdate, 0, 4);
            $cond['month'] = substr($sdate, 4, 2);
        }
        $count = M("ins_award")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("ins_award")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('id desc')->select();
        $room_id_arr = array();
        $room_arr = array();
        foreach ($list as $val) {
            $room_id_arr[] = $val['room_id'];
        }
        if (!empty($room_id_arr)) {
            $room_id_arr = array_unique($room_id_arr);
            $cond_room['id'] = array("IN", $room_id_arr);
            $room_arr = M("ins_room")->where($cond_room)->getField("id,name");
        }
        foreach ($list as &$val) {
            if (array_key_exists($val['room_id'], $room_arr)) {
                $val['room_name'] = $room_arr[$val['room_id']];
            } else {
                $val['room_name'] = "";
            }
        }
        $this->assign("list", $list);

        $cond_room_all['status'] = 1;
        $room = M("ins_room")->where($cond_room_all)->select();
        $this->assign("room", $room);
        $this->display();
    }

    public function awardUpdate()
    {
        $params = I("get.", "", "");
        $data['room_id'] = $params['room_id'];
        $data['award'] = $params['award'];
        $data['fee'] = $params['fee'];
        $data['year'] = substr($params['date'], 0, 4);
        $data['month'] = substr($params['date'], 5, 2);
        if (empty($params['id'])) {
            $cond_added['room_id'] = $data['room_id'];
            $cond_added['year'] = $data['year'];
            $cond_added['month'] = $data['month'];
            $cond_added['status'] = array("NEQ", 9);
            $data_added = M("ins_award")->where($cond_added)->find();
            if (!empty($data_added)) {
                $this->ajaxReturn(['code' => -1, 'msg' => '此部室本月已设置过奖金']);
            }
            M("ins_award")->add($data);
        } else {
            $cond['id'] = $params['id'];
            M("ins_award")->where($cond)->save($data);
        }

        $this->ajaxReturn(['code' => 1, 'msg' => '成功']);
    }

    public function awardDelete()
    {
        $id = I("get.id", "", "");
        $data['status'] = 9;
        if (!empty($id)) {
            M("ins_award")->where(['id' => $id])->save($data);
        }
        return $this->my_success("成功");
    }

    public function awardImport()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 200 * 1024 * 1024;// 设置附件上传大小
        $upload->exts = array("xlsx", "xls");// 设置附件上传类型
        $upload->rootPath = 'Public/Uploads/tool/award/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
            vendor('phpexcel.PHPExcel');
            $objExcel = new \PHPExcel();
            $file = 'Public/Uploads/tool/award/' . date("Y-m-d") . "/" . $info['import']['savename'];
            $type = pathinfo($file);
            $type = strtolower($type["extension"]);
            $type = $type === 'csv' ? $type : 'Excel2007';     //这里需要与格式对应
            ini_set('max_execution_time', '0');

            // 判断使用哪种格式
            $objReader = \PHPExcel_IOFactory::createReader($type);
            $objPHPExcel = $objReader->load($file);
            $sheet = $objPHPExcel->getSheet(0);
            $allColumn = $sheet->getHighestColumn();
            $allRow = $sheet->getHighestRow();        //**取得一共有多少行*/

            $data = array();
            for ($rowIndex = 1; $rowIndex <= $allRow; $rowIndex++) {        //循环读取每个单元格的内容。注意行从1开始，列从A开始
                $data_unit = array();
                for ($colIndex = 'A'; $colIndex <= $allColumn; $colIndex++) {
                    $addr = $colIndex . $rowIndex;
                    $cell = $sheet->getCell($addr)->getValue();
                    if ($cell instanceof \PHPExcel_RichText) { //富文本转换字符串
                        $cell = $cell->__toString();
                    }
                    switch ($colIndex) {
                        case "A":
                            $data_unit['name'] = $cell;
                            break;
                        case "B":
                            $data_unit['award'] = $cell;
                            break;
                        case "C":
                            $data_unit['fee'] = $cell;
                            break;
                        case "D":
                            $data_unit['year'] = $cell;
                            break;
                        case "E":
                            $data_unit['month'] = $cell;
                            break;
                        default:
                            break;
                    }
                }
                $cond_room['name'] = $data_unit['name'];
                $cond_room['status'] = 1;
                $room = M("ins_room")->where($cond_room)->find();
                if (!empty($room)) {
                    $data_unit['room_id'] = $room['id'];
                    $cond_award['room_id'] = $room['id'];
                    $cond_award['status'] = 1;
                    $cond_award['year'] = $data_unit['year'];
                    $cond_award['month'] = $data_unit['month'];
                    $room_award = M("ins_award")->where($cond_award)->find();
                    if (empty($room_award)) {
                        M("ins_award")->add($data_unit);
                    } else {
                        M("ins_award")->where($cond_award)->save($data_unit);
                    }
                }
            }
            unlink($file);
            @rmdir('Public/Uploads/tool/award/' . date("Y-m-d") . "/");
        }
        $this->redirect("/User/Tool/award");
    }
    //--------分配管理结束-------------------------

    //----------奖金分配明细开始-------------------
    public function ajaxAwardDtl()
    {
        $cond['status'] = array("NEQ", 9);
        $cond_room['id'] = $cond['room_id'] = I("get.room_id", "", "");
        $room = M("ins_room")->where($cond_room)->find();
        $this->assign("room", $room);
        $cond['month'] = I("get.month", "", "");
        $cond['year'] = I("get.year", "", "");
        $list = M("ins_award_dtl")->where($cond)->order('id asc')->select();
        $emp_id_arr = array();
        $emp_arr = array();
        foreach ($list as $val) {
            $emp_id_arr[] = $val['emp_id'];
        }
        if (!empty($emp_id_arr)) {
            $emp_id_arr = array_unique($emp_id_arr);
            $cond_emp['id'] = array("IN", $emp_id_arr);
            $emp_arr = M("ins_employee")->where($cond_emp)->getField("id,name");
        }
        foreach ($list as &$val) {
            if (array_key_exists($val['emp_id'], $emp_arr)) {
                $val['emp_name'] = $emp_arr[$val['emp_id']];
            } else {
                $val['emp_name'] = "";
            }
        }
        $this->ajaxReturn($list);
    }

    public function awardDtl()
    {
        $cond['status'] = array("NEQ", 9);
        $cond_room['id'] = $cond['room_id'] = I("get.room_id", "", "");
        $room = M("ins_room")->where($cond_room)->find();
        $this->assign("room", $room);
        $cond['month'] = I("get.month", "", "");
        $cond['year'] = I("get.year", "", "");
        $count = M("ins_award_dtl")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("ins_award_dtl")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('id asc')->select();
        $emp_id_arr = array();
        $emp_arr = array();
        foreach ($list as $val) {
            $emp_id_arr[] = $val['emp_id'];
        }
        if (!empty($emp_id_arr)) {
            $emp_id_arr = array_unique($emp_id_arr);
            $cond_emp['id'] = array("IN", $emp_id_arr);
            $emp_arr = M("ins_employee")->where($cond_emp)->getField("id,name");
        }
        foreach ($list as &$val) {
            if (array_key_exists($val['emp_id'], $emp_arr)) {
                $val['emp_name'] = $emp_arr[$val['emp_id']];
            } else {
                $val['emp_name'] = "";
            }
        }
        $this->assign("list", $list);

        $this->display();
    }

    public function exportAwardDtl()
    {
        set_time_limit(0);
        vendor('phpexcel.PHPExcel');
        $objExcel = new \PHPExcel();
        $objActSheet = $objExcel->getActiveSheet();
        $objActSheet->setCellValue('A1', "部室名称");
        $objActSheet->setCellValue('B1', "员工名称");
        $objActSheet->setCellValue('C1', "年");
        $objActSheet->setCellValue('D1', "月");
        $objActSheet->setCellValue('E1', "金额");
        $objActSheet->setCellValue('F1', "状态");
        $line = 2;
        $cond['status'] = array("NEQ", 9);
        $sdate = I("get.sdate", "", "");
        if (!empty($sdate)) {
            $cond['year'] = substr($sdate, 0, 4);
            $cond['month'] = substr($sdate, 4, 2);
        } else {
            $cond['year'] = date("Y");
            $cond['month'] = date("n");
        }
        $ins_award_list = M("ins_award")->where($cond)->select();
        $count = M("ins_award_dtl")->where($cond)->count();
        if ($count > 0) {
            $limit = $count / 100;
            for ($i = 0; $i < $limit; $i++) {
                $start = $i * 100;
                $dtls = M("ins_award_dtl")->where($cond)->limit($start, 100)->order("id desc")->select();
                $room_id_arr = array();
                $emp_id_arr = array();
                foreach ($dtls as $dtl) {
                    $room_id_arr[] = $dtl['room_id'];
                    $emp_id_arr[] = $dtl['emp_id'];
                }
                $room_arr = array();
                $emp_arr = array();
                if (!empty($room_id_arr)) {
                    $room_id_arr = array_unique($room_id_arr);
                    $cond_room['id'] = array("IN", $room_id_arr);
                    $room_arr = M("ins_room")->where($cond_room)->getField("id,name,status");
                }
                if (!empty($emp_id_arr)) {
                    $emp_id_arr = array_unique($emp_id_arr);
                    $cond_emp['id'] = array("IN", $emp_id_arr);
                    $emp_arr = M("ins_employee")->where($cond_emp)->getField("id,name");
                }
                foreach ($dtls as $dtl) {
                    $room_name = array_key_exists($dtl['room_id'], $room_arr) ? $room_arr[$dtl['room_id']]['name'] : "";
                    $objActSheet->setCellValue('A' . $line, $room_name);

                    $emp_name = array_key_exists($dtl['emp_id'], $emp_arr) ? $emp_arr[$dtl['emp_id']] : "";
                    $objActSheet->setCellValue('B' . $line, $emp_name);

                    $objActSheet->setCellValue('C' . $line, $dtl['year']);
                    $objActSheet->setCellValue('D' . $line, $dtl['month']);
                    $objActSheet->setCellValue('E' . $line, $dtl['award']);
                    $status = 0;
                    foreach ($ins_award_list as $ins_award){
                        if($ins_award['room_id'] == $dtl['room_id']){
                            $status = $ins_award['status'];
                        }
                        $ins_award['remain_status'] = $ins_award['status'];
                        if($ins_award['status'] != 2){
                            M("ins_award")->save($ins_award);
                        }
                    }
                    switch ($status) {
                        case "":
                            $status = "未知";
                            break;
                        case 1:
                            $status = "待提交";
                            break;
                        case 2:
                            $status = "已导出";
                            break;
                        case 3:
                            $status = "待审核";
                            break;
                        case 4:
                            $status = "待审批";
                            break;
                        case 5:
                            $status = "已完成";
                            break;
                    }
                    $objActSheet->setCellValue('F' . $line, $status);
                    $line++;
                }
            }
        }
        $cond_award['year'] = $cond['year'];
        $cond_award['month'] = $cond['month'];
        $cond_award['status'] = array("NEQ", 9);
        $data['status'] = 2;
        M("ins_award")->where($cond_award)->save($data);
        $fileName = '奖金分配详情' . date('Ymd') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
        $objWriter->save('php://output');
    }

    public function awardDtlDelete()
    {
        $id = I("get.id", "", "");
        $data['status'] = 9;
        if (!empty($id)) {
            M("ins_award_dtl")->where(['id' => $id])->save($data);
        }
        return $this->my_success("成功");
    }

    public function awardRenew()
    {
        $id = I("get.id", "", "");
        $data['status'] = 1;
        if (!empty($id)) {
            M("ins_award")->where(['id' => $id])->save($data);
        }
        return $this->my_success("成功");
    }
    //----------奖金分配明细结束-------------------

    //--------部室资金明细开始-------------------------
    public function riches()
    {
        $cond['status'] = array("NEQ", 9);
        $room_id = I("get.room_id", "", "");
        if (!empty($room_id)) {
            $cond['room_id'] = $room_id;
        }
        $sdate = I("get.sdate", "", "");
        if (!empty($sdate)) {
            $cond['year'] = substr($sdate, 0, 4);
            $cond['month'] = substr($sdate, 4, 2);
        }
        $count = M("ins_riches")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("ins_riches")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('id desc')->select();
        $room_id_arr = array();
        $room_arr = array();
        foreach ($list as $val) {
            $room_id_arr[] = $val['room_id'];
        }
        if (!empty($room_id_arr)) {
            $room_id_arr = array_unique($room_id_arr);
            $cond_room['id'] = array("IN", $room_id_arr);
            $room_arr = M("ins_room")->where($cond_room)->getField("id,name");
        }
        foreach ($list as &$val) {
            if (array_key_exists($val['room_id'], $room_arr)) {
                $val['room_name'] = $room_arr[$val['room_id']];
            } else {
                $val['room_name'] = "";
            }
        }
        $this->assign("list", $list);

        $cond_room_all['status'] = 1;
        $room = M("ins_room")->where($cond_room_all)->select();
        $this->assign("room", $room);
        $this->display();
    }


    public function richesDelete()
    {
        $id = I("get.id", "", "");
        $data['status'] = 9;
        if (!empty($id)) {
            M("ins_award")->where(['id' => $id])->save($data);
        }
        return $this->my_success("成功");
    }

    public function richesImport()
    {
        set_time_limit(0);
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 200 * 1024 * 1024;// 设置附件上传大小
        $upload->exts = array("xlsx", "xls");// 设置附件上传类型
        $upload->rootPath = 'Public/Uploads/tool/award/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
            vendor('phpexcel.PHPExcel');
            $objExcel = new \PHPExcel();
            $file = 'Public/Uploads/tool/award/' . date("Y-m-d") . "/" . $info['import']['savename'];
            $type = pathinfo($file);
            $type = strtolower($type["extension"]);
            $type = $type === 'csv' ? $type : 'Excel2007';     //这里需要与格式对应

            // 判断使用哪种格式
            $objReader = \PHPExcel_IOFactory::createReader($type);
            $objPHPExcel = $objReader->load($file);
            $sheet = $objPHPExcel->getSheet(0);
            $allColumn = $sheet->getHighestColumn();
            $allRow = $sheet->getHighestRow();        //**取得一共有多少行*/
            for ($rowIndex = 2; $rowIndex <= $allRow; $rowIndex++) {        //循环读取每个单元格的内容。注意行从1开始，列从A开始
                $data_unit = array();
                for ($colIndex = 'A'; $colIndex <= $allColumn; $colIndex++) {
                    $addr = $colIndex . $rowIndex;
                    $cell = $sheet->getCell($addr)->getValue();
                    if ($cell instanceof \PHPExcel_RichText) { //富文本转换字符串
                        $cell = $cell->__toString();
                    }
                    echo $cell . "</br>";
                    switch ($colIndex) {
                        case "A":
                            $data_unit['name'] = $cell;
                            break;
                        case "B":
                            $data_unit['m_start'] = $cell;
                            break;
                        case "C":
                            $data_unit['m_end'] = $cell;
                            break;
                        case "D":
                            $data_unit['year'] = $cell;
                            break;
                        case "E":
                            $data_unit['month'] = $cell;
                            break;
                        default:
                            $addr = $colIndex . 1;
                            $cell_name = $sheet->getCell($addr)->getValue();
                            if ($cell_name instanceof \PHPExcel_RichText) { //富文本转换字符串
                                $cell_name = $cell_name->__toString();
                            }
                            if ($cell == 0) {
                                break;
                            }
                            if ($cell > 0) {
                                $data_unit['income'] .= $cell_name . ":" . $cell . ";";
                            } else {
                                $data_unit['pay'] .= $cell_name . ":" . $cell . ";";
                            }
                            break;
                    }
                }
                $cond_room['name'] = $data_unit['name'];
                $cond_room['status'] = 1;
                $room = M("ins_room")->where($cond_room)->find();
                if (!empty($room)) {
                    $data_unit['room_id'] = $room['id'];
                    $cond_riches['room_id'] = $room['id'];
                    $cond_riches['status'] = 1;
                    $cond_riches['year'] = $data_unit['year'];
                    $cond_riches['month'] = $data_unit['month'];
                    $room_riches = M("ins_riches")->where($cond_riches)->find();
                    if (empty($room_riches)) {
                        M("ins_riches")->add($data_unit);
                    } else {
                        M("ins_riches")->where($cond_riches)->save($data_unit);
                    }
                }
            }
            unlink($file);
            @rmdir('Public/Uploads/tool/award/' . date("Y-m-d") . "/");
        }

        $this->redirect("/User/Tool/riches");
    }
    //--------部室资金明细结束-------------------------
}

?>
